package com.xinghou.platform.server.api.controller;

import com.xinghou.platform.dao.bases.util.StringUtils;
import com.xinghou.platform.entity.req.ListReq;
import com.xinghou.platform.entity.req.UserInviteReq;
import com.xinghou.platform.entity.resp.UserInfoResp;
import com.xinghou.platform.model.main.UserInvite;
import com.xinghou.platform.server.api.bussiness.UserInviteService;
import com.xinghou.platform.util.exception.DBExceptionEnums;
import com.xinghou.platform.util.exception.ServiceException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by qingshengzheng on 16/9/9.
 */
@Controller
public class UserInviteController extends BaseControl {
    @Resource
    private UserInviteService userInviteService;

    @ResponseBody
    @RequestMapping(value = "/userdrawprize", method = {RequestMethod.GET, RequestMethod.POST})
    public String userdrawprize(@RequestBody UserInviteReq userInviteReq) {
        if (StringUtils.isNullOrEmpty(userInviteReq.getUserid()))
            throw new ServiceException(DBExceptionEnums.USER_IS_NULL);
       // UserInviteResp resp = userInviteService.drawPrize(Integer.parseInt(userInviteReq.getUserid()));
        return response(null);
    }

    @ResponseBody
    @RequestMapping(value = "/userinviterank", method = {RequestMethod.GET, RequestMethod.POST})
    public String userinviterank(@RequestBody UserInviteReq userInviteReq) {
        if (StringUtils.isNullOrEmpty(userInviteReq.getUserid()))
            throw new ServiceException(DBExceptionEnums.USER_IS_NULL);

         List<UserInfoResp> list = userInviteService.getRank(Integer.parseInt(userInviteReq.getUserid()));
        return response(list);
    }

    @ResponseBody
    @RequestMapping(value = "/getUserInviteInfo", method = {RequestMethod.GET, RequestMethod.POST})
    public String getUserInviteInfo(@RequestBody UserInviteReq userInviteReq) {
        if (StringUtils.isNullOrEmpty(userInviteReq.getUserid()))
            throw new ServiceException(DBExceptionEnums.USER_IS_NULL);
        UserInfoResp userInfoResp = userInviteService.getIndexAndScoreByUser(Integer.parseInt(userInviteReq.getUserid()));
        String code = userInviteService.getInviteCodeByUser(Integer.parseInt(userInviteReq.getUserid()));
        userInfoResp.setInvitecode(code);
        return response(userInfoResp);
    }

    @ResponseBody
    @RequestMapping(value = "/getInvitedUser", method = {RequestMethod.GET, RequestMethod.POST})
    public String getInvitedUser(@RequestBody ListReq listReq) {
        if (StringUtils.isNullOrEmpty(listReq.getUserid()))
            throw new ServiceException(DBExceptionEnums.USER_IS_NULL);
        List<UserInfoResp> list = userInviteService.getInvitedUser(listReq);
        return response(list);
    }


    @ResponseBody
    @RequestMapping(value = "/addmj", method = {RequestMethod.GET, RequestMethod.POST})
    public String addmj(@RequestBody ListReq listReq) {
        if (StringUtils.isNullOrEmpty(listReq.getUserid()))
            throw new ServiceException(DBExceptionEnums.USER_IS_NULL);
            UserInvite userInvite = new UserInvite();
        List<Integer> list = new ArrayList<Integer>();
        for(int i =0;i< 100;i++){
            list.add((int)(Math.random()*7000000));
        }
        for(Integer x:list){
            int index=  (int)(Math.random()*30);
            for(int j=0;j<index;j++) {
                userInvite.setUid((int)(Math.random()*7000000));
                userInvite.setInviteuid(x);
                userInvite.setCreatetime(System.currentTimeMillis());
                userInviteService.addUserInvite(userInvite);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
        return "success";
    }
}
